var mongoose = require("mongoose");

var logger = new require("../log/Logger")("MongoDB");

var isInited = false;
var MongoDB = {
    isInited: function() {
        return isInited;
    },
    getMongoose: function() {
        return mongoose;
    },
    initDBConnection: function() {
        return new Promise(function(resolve, reject) {
            mongoose.connect('mongodb://dev.api.shaozi.com/log_service');
            var db = mongoose.connection;
            db.on('error', function(err) {
                isInited = false;
                logger.error(err);
                if (reject) {
                    reject();
                }
            });

            db.once('open', function() {
                isInited = true;
                if (resolve) {
                    resolve();
                }
            });
        });
    }
};

module.exports = MongoDB;
